package com.xinchuang.domain.vo.byd;

import com.xinchuang.domain.type.NormalOutQuotationaSubEnum;
import com.xinchuang.utils.BigdecimalUtil;
import com.xinchuang.utils.CheckUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Description:设计费
 * @Author: thh
 * @Date: 2023/9/23 19:39
 */
@Data
public class DesignCostVo {

    //小计
    private double subTotal;
    //数据
    private List<DesignCostVo.DataList> dataList;

    /**
     * 校验数据的真实性
     *
     * @return
     */
    public void checkIsEqual() {
        //校验明细
        this.dataList.stream().allMatch(dataList -> {
            String type = NormalOutQuotationaSubEnum.DESIGN_FEE.getCn() + " " + dataList.getItemName() + " 金额";
            return CheckUtil.checkIsEqual(type, dataList.getTotalPrice(), BigdecimalUtil.calculateTotalPrice(dataList.getPrice(), dataList.getHour()).doubleValue());
        });

        //校验总价
        double subTotal = this.getSubTotal();
        double sum = this.dataList.stream().map(x -> BigDecimal.valueOf(x.getTotalPrice()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO,BigDecimal::add).doubleValue();
        CheckUtil.checkIsEqual(NormalOutQuotationaSubEnum.DESIGN_FEE.getCn() + " 小计", subTotal, sum);
    }

    @Data
    @AllArgsConstructor
    public class DataList {
        //名称
        private String itemName;
        //工时(小时)
        private double hour;
        //单价(元)
        private double price;
        //金额(元)
        private double totalPrice;
    }
}
